<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="接口自动化测试requests, Zhou•Yong">
    <meta name="description" content="接口自动化测试流程
需求分析
请求（url、方法、数据）
响应（响应数据、状态码）


挑选需要做自动化接口（时间、人员、接口复杂度）
设计自动化测试用例（如果功能阶段设计过用例，直接拿过来使用即可）
搭建自动化测试环境（实现自动化使用的语">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>接口自动化测试requests | Zhou•Yong</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

</head>

<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">Zhou•Yong</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>主页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>个人介绍</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>推荐</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Zhou•Yong</div>
        <div class="logo-desc">
            
            你迈出的步伐不非得很大，只要你是在正确的方向前进就行
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                主页
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                个人介绍
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                推荐
            </a>
        </li>
        
        
    </ul>

    <div class="social-link"><a href="#!" class="tooltipped" data-tooltip="邮件联系我: zy20081@126.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 314153271" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
</div>

            </div>
        </div>

        
    </nav>
</header>





<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/16.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        接口自动化测试requests
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1,
    #articleContent h2,
    #articleContent h3,
    #articleContent h4,
    #articleContent h5,
    #articleContent h6 {
        padding-top: 76px;
        margin-top: -76px;
    }

    #articleContent h1 {
        line-height: 3.5rem;
    }

    #articleContent h2 {
        line-height: 3.2rem;
    }

    #articleContent h3 {
        line-height: 2.8rem;
    }

    #articleContent h4 {
        line-height: 2.5rem;
    }

    #articleContent h5 {
        line-height: 2.2rem;
    }

    #articleContent h6 {
        line-height: 1.9rem;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/接口自动化测试requests/" target="_blank">
                    <span class="chip bg-color">接口自动化测试requests</span>
                </a>
                
            </div>
            
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/测试/" class="post-category" target="_blank">
                        测试
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2020-12-08
                </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="接口自动化测试流程"><a href="#接口自动化测试流程" class="headerlink" title="接口自动化测试流程"></a>接口自动化测试流程</h2><ol>
<li>需求分析<ul>
<li>请求（url、方法、数据）</li>
<li>响应（响应数据、状态码）</li>
</ul>
</li>
<li>挑选需要做自动化接口（时间、人员、接口复杂度）</li>
<li>设计自动化测试用例（如果功能阶段设计过用例，直接拿过来使用即可）</li>
<li>搭建自动化测试环境（实现自动化使用的语言 如：(python、pycharm)）</li>
<li>设计自动化执行框架（报告、参数化、用例执行框架）</li>
<li>编写代码</li>
<li>执行用例(unittest、pytest)</li>
<li>生成测试报告(htmltextrunn er\allure)</li>
</ol>
<h2 id="接口用例"><a href="#接口用例" class="headerlink" title="接口用例"></a>接口用例</h2><p>单条用例<br><img src="/2020/12/08/jie-kou-zi-dong-hua-ce-shi/1.jpg" alt="图1"><br>多条用例<br><img src="/2020/12/08/jie-kou-zi-dong-hua-ce-shi/2.jpg" alt="图2"></p>
<h2 id="接口清单"><a href="#接口清单" class="headerlink" title="接口清单"></a>接口清单</h2><pre class=" language-bash"><code class="language-bash">1.1 请求登陆接口
   请求
    1<span class="token punctuation">)</span>. 请求url:http://XXXX/app/v1_0/authorizations
    2<span class="token punctuation">)</span>. 请求方法：post
    3<span class="token punctuation">)</span>. 请求参数：Headers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"Content-Type"</span><span class="token keyword">:</span> <span class="token string">"application/json    "</span><span class="token punctuation">}</span>
    4<span class="token punctuation">)</span>. 请求报文：
    <span class="token punctuation">{</span><span class="token string">"mobile"</span><span class="token keyword">:</span> <span class="token string">"18600000000"</span>,<span class="token string">"code"</span><span class="token keyword">:</span> 888888<span class="token punctuation">}</span>
   响应
    1<span class="token punctuation">)</span>. 状态码：201
    2<span class="token punctuation">)</span>. 响应数据
    <span class="token punctuation">{</span><span class="token string">"message"</span><span class="token keyword">:</span><span class="token string">"xxx"</span><span class="token punctuation">}</span>
</code></pre>
<h2 id="requests"><a href="#requests" class="headerlink" title="requests"></a>requests</h2><p>pip install requests<br>基于Requests库，集成UnitTest，接口测试框架开发<br><img src="/2020/12/08/jie-kou-zi-dong-hua-ce-shi/3.jpg" alt="图3"></p>
<h2 id="接口自动化测试代码"><a href="#接口自动化测试代码" class="headerlink" title="接口自动化测试代码"></a>接口自动化测试代码</h2><p>下面写一个Login案例</p>
<h3 id="run-suite-py主入口"><a href="#run-suite-py主入口" class="headerlink" title="run_suite.py主入口"></a>run_suite.py主入口</h3><pre class=" language-bash"><code class="language-bash">这里还是用的三方HTMLTestRunner生成报告，跟web自动化一样的
<span class="token string">""</span><span class="token string">"
    目标：
        1. 搜索组装测试套件
        2. 运行测试套件并生成测试报告
"</span><span class="token string">""</span>

<span class="token comment" spellcheck="true"># 导包 unittest HTMLTestRunner time</span>
<span class="token function">import</span> unittest
<span class="token function">import</span> <span class="token function">time</span>
from tools.HTMLTestRunner <span class="token function">import</span> HTMLTestRunner


<span class="token comment" spellcheck="true"># 第一步：组装测试套件</span>
suite <span class="token operator">=</span> unittest.defaultTestLoader.discover<span class="token punctuation">(</span><span class="token string">"./case"</span>,pattern<span class="token operator">=</span><span class="token string">"test*.py"</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 第二步：指定报告存放路径及文件名称</span>
file_path <span class="token operator">=</span> <span class="token string">"./report/{}.html"</span>.format<span class="token punctuation">(</span>time.strftime<span class="token punctuation">(</span><span class="token string">"%Y_%m_%d %H_%M_%S"</span><span class="token punctuation">))</span>

<span class="token comment" spellcheck="true"># 第三步：运行测试套件并生成测试报告</span>
with open<span class="token punctuation">(</span>file_path, <span class="token string">"wb"</span><span class="token punctuation">)</span> as f:
    HTMLTestRunner<span class="token punctuation">(</span>stream<span class="token operator">=</span>f<span class="token punctuation">)</span>.run<span class="token punctuation">(</span>suite<span class="token punctuation">)</span>
</code></pre>
<h3 id="tools"><a href="#tools" class="headerlink" title="tools"></a>tools</h3><h4 id="read-json-py"><a href="#read-json-py" class="headerlink" title="read_json.py"></a>read_json.py</h4><pre class=" language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 导包 json</span>
<span class="token function">import</span> json

<span class="token comment" spellcheck="true"># 打开josn文件并获取文件流</span>
<span class="token comment" spellcheck="true"># with open("../data/login.json", "r", encoding="utf-8") as f:</span>
<span class="token comment" spellcheck="true">#     # 调用load方法加载文件流</span>
<span class="token comment" spellcheck="true">#     data = json.load(f)</span>
<span class="token comment" spellcheck="true">#     print("获取的数据为：", data)</span>

<span class="token comment" spellcheck="true"># 使用函数进行封装</span>
<span class="token comment" spellcheck="true"># def read_json():</span>
<span class="token comment" spellcheck="true">#     with open("../data/login.json", "r", encoding="utf-8") as f:</span>
<span class="token comment" spellcheck="true">#         # 调用load方法加载文件流</span>
<span class="token comment" spellcheck="true">#         return json.load(f)</span>


<span class="token comment" spellcheck="true"># 使用参数替换 静态文件名</span>

class ReadJson<span class="token punctuation">(</span>object<span class="token punctuation">)</span>:

    def __init__<span class="token punctuation">(</span>self, filename<span class="token punctuation">)</span>:
        self.filepath <span class="token operator">=</span> <span class="token string">"../data/"</span> + filename

    def read_json<span class="token punctuation">(</span>self<span class="token punctuation">)</span>:
        with open<span class="token punctuation">(</span>self.filepath, <span class="token string">"r"</span>, encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span> as f:
            <span class="token comment" spellcheck="true"># 调用load方法加载文件流</span>
            <span class="token keyword">return</span> json.load<span class="token punctuation">(</span>f<span class="token punctuation">)</span>



<span class="token string">""</span><span class="token string">"
    问题：
        1. 未经过封装无法在别的模块内使用。
        2. 数据存储文件有好几个，如果写次，在读取别的文件时无法使用
        3. 预期格式为列表嵌套元祖 [(url,mobile,code...)],目前返回字典？
    解决：
        1. 进行封装
        2. 使用参数替换静态写死的文件名
        3. 读取字典内容，并添加到新的列表中。
"</span><span class="token string">""</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token keyword">:</span>
    <span class="token comment" spellcheck="true"># print(ReadJson("login.json").read_json())</span>


    <span class="token comment" spellcheck="true"># 登录数据调试</span>
    <span class="token comment" spellcheck="true"># data = ReadJson("login.json").read_json()</span>
    <span class="token comment" spellcheck="true"># 新建空列表，添加读取json数据</span>
    <span class="token comment" spellcheck="true"># arrs = []</span>
    <span class="token comment" spellcheck="true"># arrs.append((data.get("url"),</span>
    <span class="token comment" spellcheck="true">#              data.get("mobile"),</span>
    <span class="token comment" spellcheck="true">#              data.get("code"),</span>
    <span class="token comment" spellcheck="true">#              data.get("expect_result"),</span>
    <span class="token comment" spellcheck="true">#              data.get("status_code")))</span>
    <span class="token comment" spellcheck="true"># print(arrs)</span>


    <span class="token comment" spellcheck="true"># 获取用户频道列表 调试</span>
    <span class="token comment" spellcheck="true"># data = ReadJson("channel.json").read_json()</span>
    <span class="token comment" spellcheck="true"># # 新建空列表，添加读取json数据</span>
    <span class="token comment" spellcheck="true"># arrs = []</span>
    <span class="token comment" spellcheck="true"># arrs.append((data.get("url"),</span>
    <span class="token comment" spellcheck="true">#              data.get("headers"),</span>
    <span class="token comment" spellcheck="true">#              data.get("expect_result"),</span>
    <span class="token comment" spellcheck="true">#              data.get("status_code")))</span>
    <span class="token comment" spellcheck="true"># print(arrs)</span>

    <span class="token comment" spellcheck="true"># 获取收藏文章 调试</span>
    <span class="token comment" spellcheck="true"># data = ReadJson("article_add.json").read_json()</span>
    <span class="token comment" spellcheck="true"># # 新建空列表，添加读取json数据</span>
    <span class="token comment" spellcheck="true"># arrs = []</span>
    <span class="token comment" spellcheck="true"># arrs.append((data.get("url"),</span>
    <span class="token comment" spellcheck="true">#              data.get("headers"),</span>
    <span class="token comment" spellcheck="true">#              data.get("data"),</span>
    <span class="token comment" spellcheck="true">#              data.get("expect_result"),</span>
    <span class="token comment" spellcheck="true">#              data.get("status_code")))</span>
    <span class="token comment" spellcheck="true"># print(arrs)</span>

    <span class="token comment" spellcheck="true"># 获取取消收藏文章 调试</span>
    data <span class="token operator">=</span> ReadJson<span class="token punctuation">(</span><span class="token string">"article_cancel.json"</span><span class="token punctuation">)</span>.read_json<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 新建空列表，添加读取json数据</span>
    arrs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    arrs.append<span class="token punctuation">((</span>data.get<span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span>,
                 data.get<span class="token punctuation">(</span><span class="token string">"headers"</span><span class="token punctuation">)</span>,
                 data.get<span class="token punctuation">(</span><span class="token string">"status_code"</span><span class="token punctuation">))</span><span class="token punctuation">)</span>
    print<span class="token punctuation">(</span>arrs<span class="token punctuation">)</span>

</code></pre>
<h3 id="data-data-login-json"><a href="#data-data-login-json" class="headerlink" title="data - data-login.json"></a>data - data-login.json</h3><pre class=" language-bash"><code class="language-bash"><span class="token punctuation">{</span>
  <span class="token string">"url"</span><span class="token keyword">:</span><span class="token string">"http://XXXXXXXXX/app/v1_0/authorizations"</span>,
  <span class="token string">"mobile"</span><span class="token keyword">:</span> <span class="token string">"13298295865"</span>,
  <span class="token string">"code"</span><span class="token keyword">:</span> <span class="token string">"390871"</span>,
  <span class="token string">"expect_result"</span><span class="token keyword">:</span> <span class="token string">"OK"</span>,
  <span class="token string">"status_code"</span><span class="token keyword">:</span> 201
<span class="token punctuation">}</span>
</code></pre>
<h3 id="api-api-login-py"><a href="#api-api-login-py" class="headerlink" title="api -api_login.py"></a>api -api_login.py</h3><pre class=" language-bash"><code class="language-bash"><span class="token string">""</span><span class="token string">"
    目标：实现登录接口对象封装
"</span><span class="token string">""</span>

<span class="token comment" spellcheck="true"># 导包 requests</span>
<span class="token function">import</span> requests


<span class="token comment" spellcheck="true"># 新建类 登录接口对象</span>
class ApiLogin<span class="token punctuation">(</span>object<span class="token punctuation">)</span>:
    <span class="token comment" spellcheck="true"># 新建方法 登录方法</span>
    def api_post_login<span class="token punctuation">(</span>self, url, mobile, code<span class="token punctuation">)</span>:
        <span class="token comment" spellcheck="true"># headers 定义</span>
        headers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"Content-Type"</span><span class="token keyword">:</span> <span class="token string">"application/json"</span><span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true"># data 定义</span>
        data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"mobile"</span><span class="token keyword">:</span> mobile, <span class="token string">"code"</span><span class="token keyword">:</span> code<span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true"># 调用post并返回响应对象</span>
        <span class="token keyword">return</span> requests.post<span class="token punctuation">(</span>url, headers<span class="token operator">=</span>headers, json<span class="token operator">=</span>data<span class="token punctuation">)</span>


<span class="token string">""</span><span class="token string">"
    提示：
        url、mobile、code:最后都需要从data数据文件读取出来，做参数化使用，所以这里我们动态传参
"</span><span class="token string">""</span>
</code></pre>
<h3 id="case-test-login-py"><a href="#case-test-login-py" class="headerlink" title="case -test_login.py"></a>case -test_login.py</h3><pre class=" language-bash"><code class="language-bash"><span class="token string">""</span><span class="token string">"
    目标：完成登录业务层实现
"</span><span class="token string">""</span>

<span class="token comment" spellcheck="true"># 导包 unittest ApiLogin</span>
<span class="token function">import</span> unittest
from api.api_login <span class="token function">import</span> ApiLogin
from parameterized <span class="token function">import</span> parameterized
from tools.read_json <span class="token function">import</span> ReadJson


<span class="token comment" spellcheck="true"># 读取数据函数</span>
def get_data<span class="token punctuation">(</span><span class="token punctuation">)</span>:
    data <span class="token operator">=</span> ReadJson<span class="token punctuation">(</span><span class="token string">"login.json"</span><span class="token punctuation">)</span>.read_json<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 新建空列表，添加读取json数据</span>
    arrs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    arrs.append<span class="token punctuation">((</span>data.get<span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span>,
                 data.get<span class="token punctuation">(</span><span class="token string">"mobile"</span><span class="token punctuation">)</span>,
                 data.get<span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">)</span>,
                 data.get<span class="token punctuation">(</span><span class="token string">"expect_result"</span><span class="token punctuation">)</span>,
                 data.get<span class="token punctuation">(</span><span class="token string">"status_code"</span><span class="token punctuation">))</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> arrs

<span class="token comment" spellcheck="true"># 新建测试类</span>
class TestLogin<span class="token punctuation">(</span>unittest.TestCase<span class="token punctuation">)</span>:
    <span class="token comment" spellcheck="true"># 新建测试方法</span>
    @parameterized.expand<span class="token punctuation">(</span>get_data<span class="token punctuation">(</span><span class="token punctuation">))</span>
    def test_login<span class="token punctuation">(</span>self, url,mobile, code, expect_result, status_code<span class="token punctuation">)</span>:
        <span class="token comment" spellcheck="true"># 暂时存放数据 url mobile code</span>
        <span class="token comment" spellcheck="true"># url = "http://XXXX/app/v1_0/authorizations"</span>
        <span class="token comment" spellcheck="true"># mobile = "18610453007"</span>
        <span class="token comment" spellcheck="true"># code = "882477"</span>

        <span class="token comment" spellcheck="true"># 调用登录方法</span>
        s <span class="token operator">=</span> ApiLogin<span class="token punctuation">(</span><span class="token punctuation">)</span>.api_post_login<span class="token punctuation">(</span>url, mobile, code<span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># 调试使用</span>
        print<span class="token punctuation">(</span><span class="token string">"查看响应结果："</span>, s.json<span class="token punctuation">(</span><span class="token punctuation">))</span>

        <span class="token comment" spellcheck="true"># 断言 响应信息 及 状态码</span>
        <span class="token comment" spellcheck="true"># self.assertEquals("OK", s.json()['message'])</span>
        self.assertEquals<span class="token punctuation">(</span>expect_result, s.json<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'message'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># 断言响应状态码</span>
        <span class="token comment" spellcheck="true"># self.assertEquals(201, s.status_code)</span>
        self.assertEquals<span class="token punctuation">(</span>status_code, s.status_code<span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token keyword">:</span>
    unittest.main<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token string">""</span><span class="token string">"
test_login.py .查看响应结果： 
{'message': 'OK', 'data': 
{'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NTAyNTA4NDIsInVzZXJfaWQiOjU3LCJyZWZyZXNoIjpmYWxzZX0.MB2ZK5c8-VJHdA40r4ijLyAmIbPMpi_40tpeo3Wj4wk', 
'refresh_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NTE0NTMyNDIsInVzZXJfaWQiOjU3LCJyZWZyZXNoIjp0cnVlfQ.EIcGWKEuGq1JPoiQEpxWR7FdZU8h78HtTDyzaT21xpg'}}

"</span><span class="token string">""</span>
</code></pre>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff;
        background-color: #22AB38;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff;
        background-color: #019FE8;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a class="reward-link btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs">
                        <li class="tab wechat-tab waves-effect waves-light"><a class="active" href="#wechat">微信</a></li>
                        <li class="tab alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                    </ul>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('#reward .reward-link').on('click', function () {
            $('#rewardModal').openModal();
        });

        $('#rewardModal .close').on('click', function () {
            $('#rewardModal').closeModal();
        });
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://zy20081.github.io" class="b-link-green">Zhou•Yong</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/2020/12/08/jie-kou-zi-dong-hua-ce-shi/" class="b-link-green">接口自动化测试requests</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">本篇</div>
            <div class="card">
                <a href="/2020/12/08/jie-kou-zi-dong-hua-ce-shi/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/16.jpg" class="responsive-img" alt="接口自动化测试requests">
                        
                        <span class="card-title">接口自动化测试requests</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">接口自动化测试流程
需求分析
请求（url、方法、数据）
响应（响应数据、状态码）


挑选需要做自动化接口（时间、人员、接口复杂度）
设计自动化测试用例（如果功能阶段设计过用例，直接拿过来使用即可）
搭建自动化测试环境（实现自动化使用的语</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2020-12-08
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/测试/" class="post-category" target="_blank">
                                    测试
                                </a>
                            
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/接口自动化测试requests/" target="_blank">
                        <span class="chip bg-color">接口自动化测试requests</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/2020/12/08/jmeter-jie-kou-xing-neng-ce-shi/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/19.jpg" class="responsive-img" alt="JMeter接口性能测试">
                        
                        <span class="card-title">JMeter接口性能测试</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">JMeter JMeter是的一款接口和性能测试工具 优点：1. 开源、免费 2. 跨平台 3. 支持多协议 4. 小巧 5. 功能强大 缺点：1. 不支持IP欺骗 2. 使用JMeter无法验证JS程序，也无法验证页面UI，所以要和Sel</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2020-12-08
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/测试/" class="post-category" target="_blank">
                                    测试
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/JMeter接口性能测试/" target="_blank">
                        <span class="chip bg-color">JMeter接口性能测试</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title">目录</div>
            <div id="toc-content">

            </div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由ZhouYong 技术分享，心得分享.
        </div>
        <div class="col s12 m4 l4 social-link"><a href="#!" class="tooltipped" data-tooltip="邮件联系我: zy20081@126.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 314153271" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input" autofocus>
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>

<script src="/libs/others/clicklove.js"></script>

<script src="/js/matery.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->


</body>
</html>